<template >
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb
      separator-class="el-icon-arrow-right"
      style="margin-bottom: 20px"
    >
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区 -->
    <el-card>
      <div>
        <el-button
          type="primary"
          style="margin-bottom: 20px"
          @click="addClassList"
          >添加分类</el-button
        >
        <tree-table
          :data="cateList"
          :columns="columns"
          :selection-type="false"
          :expand-type="false"
          :show-index="true"
          index-text="#"
          border
          stripe
        >
          <!-- 是否有效 -->
          <template slot="isok" slot-scope="scope">
            <i
              class="el-icon-success"
              style="color: green"
              v-if="scope.row.cat_deleted === false"
            ></i>
            <i class="el-icon-error" style="color: red" v-else></i>
          </template>
          <!-- 排序 -->
          <template slot="order" slot-scope="{ row }">
            <el-tag type="success" v-if="row.cat_level == 0">1级</el-tag>
            <el-tag type="primary" v-else-if="row.cat_level == 1">2级</el-tag>
            <el-tag type="warning" v-else>3级</el-tag>
          </template>
          <!-- 操作 -->
          <template slot="opt" slot-scope="{ row }">
            <el-button
              type="primary"
              class="el-icon-edit"
              size="mini"
              @click="editshowDialog(row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              class="el-icon-delete"
              size="mini"
              @click="deleteClassList(row.cat_id)"
              >删除</el-button
            >
          </template>
        </tree-table>
      </div>
      <!-- 分页器 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 15px"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 10, 100]"
        :page-size="queryInfo.pagesize"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加对话框Dialog -->
    <el-dialog
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="30%"
      @close="addCloseDialog"
    >
      <el-form
        :model="addRuleForm"
        :rules="addClassificationRules"
        ref="addRuleFormRef"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addRuleForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- 级联选择器 -->
          <el-cascader
            v-model="selectOptions"
            :options="parentList"
            :props="{
              expandTrigger: 'hover',
              value: 'cat_id',
              label: 'cat_name',
              children: 'children',
              checkStrictly: true,
            }"
            @change="selectChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addGoodsClass">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑分类Diglog -->
    <el-dialog title="编辑分类" :visible.sync="editdialogVisible" width="30%">
      <el-form ref="editformRef" :model="editForm" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="editForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClassList">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 商品分类数据
      cateList: [],
      queryInfo: {
        pagenum: 1,
        pagesize: 5,
        type: 3,
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok',
        },
        {
          label: '排序',
          type: 'template',
          template: 'order',
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
        },
      ],
      addDialogVisible: false,
      addClassificationRules: {
        cat_name: [{ required: true, message: '请输乳名称', trigger: 'blur' }],
      },
      addRuleForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0,
      },
      selectOptions: [],
      parentList: [],
      editdialogVisible: false,
      editForm: {
        cat_name: '',
      },
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    //   获取商品分类的数据
    async getCateList() {
      let { data: res } = await this.$http.get('categories', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取数据失败')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    //分页器
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getCateList()
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getCateList()
    },
    async getParentList() {
      let { data: res } = await this.$http.get('categories', { type: '2' })
      if (res.meta.status !== 200) {
        return this.$message.error('获取数据失败')
      }

      this.parentList = res.data
    },
    //添加分类对话框
    addClassList() {
      this.getParentList()
      this.addDialogVisible = true
    },
    selectChange() {
      if (this.selectOptions.length > 0) {
        this.addRuleForm.cat_pid =
          this.selectOptions[this.selectOptions.length - 1]
        this.addRuleForm.cat_level = this.selectOptions.length
        return
      } else {
        this.addRuleForm.cat_level = 0
        this.addRuleForm.cat_pid = 0
      }
    },
    //添加分类
    addGoodsClass() {
      this.$refs.addRuleFormRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.error('请输入合法的名称')
        }
        let { data: res } = await this.$http.post(
          'categories',
          this.addRuleForm
        )
        if (res.meta.status !== 201) {
          return this.$message.error('添加失败')
        }
        this.$message.success('添加成功')
        this.getCateList()
      })

      this.addDialogVisible = false
    },
    addCloseDialog() {
      this.$refs.addRuleFormRef.resetFields()
      this.selectOptions = []
      this.addRuleForm.cat_level = 0
      this.addRuleForm.cat_pid = 0
    },
    //编辑分类
    async editshowDialog(row) {
      let { data: res } = await this.$http.get(`categories/${row.cat_id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取数据失败')
      }
      this.editForm = res.data
      this.editdialogVisible = true
    },
    //提交修改
    async editClassList() {
      console.log(this.editForm)
      let { data: res } = await this.$http.put(
        `categories/${this.editForm.cat_id}`,
        {
          cat_name: this.editForm.cat_name,
        }
      )
      if (res.meta.status !== 200) {
        return this.$message.error('修改失败')
      }
      this.$message.success('修改成功')
      this.getCateList()
      this.editdialogVisible = false
    },
    async deleteClassList(id) {
      let resDelete = await this.$confirm(
        '此操作将永久删除该, 是否继续?',
        '删除提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      if (resDelete == 'cancel') {
        this.$message.info('已取消shanchu')
        return
      }
      let { data: res } = await this.$http.delete(`categories/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.$message.success('删除完毕')
      this.getCateList()
    },
  },
}
</script>
<style lang="less" >
.el-cascader-menu {
  height: 300px;
}
</style>